# -*- coding: utf-8 -*-
# @Date   : 2020/01/08
# @File   : IC_Past1MonSelfQueryCnt_ZBC.py
# @Author : yanhaopeng

'''eof
name:最近1个月内的查询次数（本人查询）
code:IC_Past1MonSelfQueryCnt_ZBC
tableName:
columnName:
groups:征信模块-准入策略
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
Factor = 查询记录汇总信息段(PC05B)·最近1个月内的查询次数(本人查询)(PC05BS05)
'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def IC_Past1MonSelfQueryCnt():
    data = IC_DSJ_2D['data']
    # 查询记录概要   [1..1]
    PQO = getTable(data, 'PQO', 1)
    if PQO is None:
        return u'缺失值'
    elif PQO == [{}]:
        return 0
    else:
        # 查询记录概要信息单元   [1..1]
        PC05 = getTable(PQO[0], 'PC05', 1)
        if PC05 is None:
            return u'缺失值'
        elif PC05 == [{}]:
            return 0
        else:
            #查询记录汇总信息段   [1..1]
            PC05B = getTable(PC05[0], 'PC05B', 1)
            if PC05B is None:
                return u'缺失值'
            elif PC05B == [{}]:
                return 0
            else:
                PC05BS05 = PC05B[0].get('PC05BS05')
                if PC05BS05 is null_type_list:
                    return 0
                else:
                    if isfloat(PC05BS05):
                        return float(PC05BS05)
                    else:
                        return u'缺失值'


result = IC_Past1MonSelfQueryCnt()

# import json
# ratingRequest = {"legalPerson": u"朱为广", "legalPersonIdCard": "510102196706108511", "creditCode": "123456",
#                  "regNumber": "12345", "relativeDate":u'2019/08/09'}
# with open('F:/hp/IC_DSJ1.json', 'r') as f:
#     IC_DSJ_2D = json.loads(f.read())['IC_DSJ']
# print IC_Past1MonSelfQueryCnt()
